package com.ease.map.control {
   import com.ease.map.Map;
   import com.ease.map.control.ui.ImageButton;
   import com.ease.map.events.FeatureEvent;
   import com.ease.map.geometry.basetype.Pixel;
   import com.ease.map.geometry.basetype.Size;

   import flash.display.Bitmap;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import com.ease.map.control.Control;

   /**
    * Control showing some arrows to be able to pan the map and zoom in/out.
    */
   public class PanZoom extends Control {

      public static var X:int = 4;
      public static var Y:int = 4;
      private var _slideFactor:int = 50;
      private var _buttons:Vector.<ImageButton> = null;
      private var _tween:Boolean;

      [Embed(source="/assets/images/north.png")]
      protected var northMiniImg:Class;

      [Embed(source="/assets/images/west.png")]
      protected var westMiniImg:Class;

      [Embed(source="/assets/images/east.png")]
      protected var eastMiniImg:Class;

      [Embed(source="/assets/images/south.png")]
      protected var southMiniImg:Class;

      [Embed(source="/assets/images/zoom-plus-mini.png")]
      protected var zoomPlusMiniImg:Class;

      [Embed(source="/assets/images/zoom-minus-mini.png")]
      protected var zoomMinusMiniImg:Class;

      [Embed(source="/assets/images/zoom-world.png")]
      protected var zoomWorldMiniImg:Class;

      [Embed(source="/assets/images/slider.png")]
      protected var sliderImg:Class;

      [Embed(source="/assets/images/zoombar.png")]
      protected var zoombarImg:Class;

      public function PanZoom(position:Pixel = null, tween:Boolean = true) {
         this._tween = tween;
         super(position);
      }

      override public function destroy():void {
         super.destroy();
         while (this.buttons.length) {
            var btn:ImageButton = this.buttons.shift();
            btn.removeEventListener(MouseEvent.CLICK, this.click);
            btn.removeEventListener(MouseEvent.DOUBLE_CLICK, this.doubleClick);
            btn.removeEventListener(MouseEvent.MOUSE_OVER, this.mouseOver);

         }
         this.buttons = null;
         this.position = null;
      }


      override public function draw():void {
         super.draw();

         var px:Pixel = this.position;

         // place the controls
         this.buttons = new Vector.<ImageButton>();

         var sz:Size = new Size(18, 18);

         var centered:Pixel = new Pixel(this.x + sz.w / 2, this.y);

         this._addButton("panup", new northMiniImg(), centered, sz);
         px.y = centered.y + sz.h;
         this._addButton("panleft", new westMiniImg(), px, sz);
         this._addButton("panright", new eastMiniImg(), px.add(sz.w, 0), sz);
         this._addButton("pandown", new southMiniImg(), centered.add(0, sz.h * 2), sz);
         this._addButton("zoomin", new zoomPlusMiniImg(), centered.add(0, sz.h * 3 + 5), sz);
         this._addButton("zoomworld", new zoomWorldMiniImg(), centered.add(0, sz.h * 4 + 5), sz);
         this._addButton("zoomout", new zoomMinusMiniImg(), centered.add(0, sz.h * 5 + 5), sz);

      }

      /**
       * Add button
       *
       * @param name
       * @param image
       * @param xy
       * @param sz
       * @param alt
       */
      public function _addButton(name:String, image:Bitmap, xy:Pixel, sz:Size, alt:String = null):void {

         var btn:ImageButton = new ImageButton(name, image, xy, sz);

         this.addChild(btn);

         btn.addEventListener(MouseEvent.CLICK, this.click);
         btn.addEventListener(MouseEvent.DOUBLE_CLICK, this.doubleClick);
         btn.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOver);
         btn.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut);

         this.buttons.push(btn);
      }

      //Events
      private function mouseOver(evt:MouseEvent):void {
         this.map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_OVER, null));
      }

      private function mouseOut(evt:MouseEvent):void {
         this.map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_OUT, null));
      }

      public function doubleClick(evt:Event):Boolean {
         evt.stopPropagation();
         return false;
      }

      public function click(evt:Event):void {
         if (!(evt.type == MouseEvent.CLICK))
            return;

         var btn:ImageButton = evt.currentTarget as ImageButton;

         switch (btn.name) {
            case "panup":
               this.map.pan(0, -100);
               break;
            case "pandown":
               this.map.pan(0, 100);
               break;
            case "panleft":
               this.map.pan(-100, 0);
               break;
            case "panright":
               this.map.pan(100, 0);
               break;
            case "zoomin":
               this.map.zoom++;
               break;
            case "zoomout":
               this.map.zoom--;
               break;
            case "zoomworld":
               this.map.zoomToMaxExtent();
               break;
         }
      }

      //Getters and setters.

      public function get slideFactor():int {
         return this._slideFactor;
      }

      public function set slideFactor(value:int):void {
         this._slideFactor = value;
      }

      public function get buttons():Vector.<ImageButton> {
         return this._buttons;
      }

      public function set buttons(value:Vector.<ImageButton>):void {
         this._buttons = value;
      }

   }
}

